package com.dkasza.qicky;

import com.dkasza.qicky.HttpServer.ClientData;
import java.net.Socket;

public class Http {
    /** Http request class.
     *  Parameters are passed to HttpRequestHandlers using this class.
     */
    public static class Request {
        Request(Socket s) {
            socket = s;
        }
        
        public static class KeyNotFoundException extends Exception {

            public KeyNotFoundException(String k) {
                key = k;
            }
            
            public String key;
        }
        
        /**
         * The method requested by the client.
         */
        public String method;
        /**
         * The URI requested by the client.
         */
        public String path;
        /**
         * The full request string sent by the client.
         */
        public String fullRequest; 
        /**
         * Socket to the client.
         * (In case you want to send something other than a string.)
         */
        public Socket socket;
        /**
         * Reference to the HttpServer's messages object.
         */
        public HttpMessages messages;
        /**
         * Data from a POST request.
         */
        public ClientData[] postData = new ClientData[0];
        /**
         * Data from a POST request.
         */
        public ClientData[] getData;

        public ClientData postData(String key) throws KeyNotFoundException{
            for(int i = 0; i < postData.length; i++) {
                if(postData[i].key.equals(key)) {
                    return postData[i];
                }
            }
            throw new KeyNotFoundException(key);
        }
        
        public ClientData getData(String key) throws KeyNotFoundException{
            for(int i = 0; i < getData.length; i++) {
                if(getData[i].key.equals(key)) {
                    return getData[i];
                }
            }
            throw new KeyNotFoundException(key);
        }
    }
    
    /** Http response class.
     *  HttpRequestHandlers use this to respond to the requests.
     */
    public static class Response {

        /**
         * true if the response is keep-alive safe
         */
        public final boolean keepAlive;
        
        /**
         * response to the client
         */
        public final String responseString;
        
        /** Main constructor for the Http.Response class.
         *
         * @param keepalive should be true if the response is keep-alive safe, otherwise false
         * @param response the response string to send to the client
         */
        public Response(boolean keepalive, String response) {
            keepAlive = keepalive;
            responseString = response;
        }
        
    }
}
